<?php
    Class Solution{
        /**
         * 674. 最长连续递增序列
         * @param Integer[] $nums
         * @return Integer
         */
        function findLengthOfLCIS674($nums) {
            $map = [];
            $count = 0;
            foreach($nums as $key => $num) {
                if(!isset($now)){
                    $now = $num;
                    $map[$count] = 1;
                }
                if(isset($nums[$key+1])){
                    if($nums[$key+1] <= $num){
                        $now = null;
                        $count++;
                    }else{
                        $map[$count]++;
                    }
                }
            }

            return max($map);
        }

        /**
         * 680. 验证回文串 II
         * @param String $s
         * @return Boolean
         */
        function validPalindrome680($s) {
            $l = 0;$r = strlen($s)-1;
            $yes1 = true;$yes2 = true;$change = false;
            while($l < $r){
                if($s[$l] != $s[$r] && !$change){
                    $change = true;
                }
                if($change){
                    if($s[$l+1] != $s[$r]){
                        $yes1 = false;
                    }
                    if($s[$l] != $s[$r-1]){
                        $yes2 = false;
                    }
                }
                $l++;
                $r--;
            }

            return $yes1 || $yes2;
        }

        /**
         * 682. 棒球比赛
         * @param String[] $operations
         * @return Integer
         */
        function calPoints682($operations) {
            $array = [];
            $i = 0;
            foreach($operations as $item) {
                switch($item) {
                    case 'C':
                        $array[$i-1] = 0;
                        $i--;
                        break;
                    case 'D':
                        $array[$i] = $array[$i-1] * 2;
                        $i++;
                        break;
                    case '+':
                        $array[$i] = $array[$i-1] + $array[$i-2];
                        $i++;
                        break;
                    default:
                        $array[$i] = $item;
                        $i++;
                }
            }

            return array_sum($array);
        }

        /**
         * 693. 交替位二进制数
         * @param Integer $n
         * @return Boolean
         */
        function hasAlternatingBits693($n) {
            $flag = null;
            while($n > 0){
                $count = $n % 4;
                if(!isset($flag)){
                    $flag = $count;
                }elseif($flag != $count){
                    return false;
                }
                $n = intval($n/4);
            }
            return $flag == 1 || $flag == 2;
        }

        /**
         * 697. 数组的度
         * @param Integer[] $nums
         * @return Integer
         */
        function findShortestSubArray697($nums) {
            $map = [];
            foreach($nums as $key => $num){
                if(!isset($map[$num])){
                    $map[$num] = [1,$key,$key];
                }else{
                    $map[$num][0]++;
                    $map[$num][2] = $key;
                }
            }
            $max   = 0;
            $count = 0;
            foreach($map as $item){
                if($item[0] > $max){
                    $max   = $item[0];
                    $count = $item[2] - $item[1] + 1;
                }elseif($item[0] == $max){
                    $count = min($count, $item[2] - $item[1] + 1);
                }
            }

            return $count;
        }
        /**
         * 704. 二分查找
         * @param Integer[] $nums
         * @param Integer $target
         * @return Integer
         */
        function search($nums, $target) {
            $left  = 0;
            $right = count($nums) - 1;
            while($left <= $right){
                $mid = intval(($left + $right) / 2);
                if($nums[$mid] == $target){
                    return $mid;
                }elseif($nums[$mid] > $target){
                    $right = $mid - 1;
                }else{
                    $left = $mid + 1;
                }
            }

            return -1;
        }

    }

/**
 * 705. 设计哈希集合
 *
 */
    class MyHashSet {

        private $_arr = [];
        /**
         */
        function __construct() {

        }

        /**
         * @param Integer $key
         * @return NULL
         */
        function add($key) {
            $this->_arr[$key] = $key;
        }

        /**
         * @param Integer $key
         * @return NULL
         */
        function remove($key) {
            unset($this->_arr[$key]);
        }

        /**
         * @param Integer $key
         * @return Boolean
         */
        function contains($key) {
            return isset($this->_arr[$key]);
        }
    }




    $operations = [1,2,2,3,1];
    $solution = new Solution();
    print_r($solution->findShortestSubArray697($operations));

?>